package com.xul.srb.core.controller.admin;


import com.xul.common.exception.Assert;
import com.xul.common.result.ResponseEnum;
import com.xul.common.result.Result;
import com.xul.srb.core.pojo.entity.IntegralGrade;
import com.xul.srb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author xul
 * @since 2021-11-06
 */

@Api(tags = "积分等级管理")
@Slf4j
// @CrossOrigin
@RestController
@RequestMapping("/admin/core/integralGrade")
public class AdminIntegralGradeController {

    @Resource
    private IntegralGradeService integralGradeService;

    @ApiOperation("查询积分等级列表")
    @GetMapping("/list")
    public Result listAll() {

        log.info("hi i'm helen");
        log.warn("warning!!!");
        log.error("it's a error");

        return Result.ok().data("list", integralGradeService.list()).message("获取列表成功");
    }

    @ApiOperation("根据id删除积分等级列表")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Integer id) {
        boolean result = integralGradeService.removeById(id);
        if (result) {
            return Result.ok().message("删除成功");
        } else {
            return Result.error().message("删除失败");
        }
    }

    @ApiOperation("新增积分等级")
    @PostMapping("/save")
    public Result save(@RequestBody IntegralGrade integralGrade) {

//        if (integralGrade.getBorrowAmount() == null) {
//            throw new BusinessException(ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
//        }
        // 使用断言
        Assert.notNull(integralGrade.getBorrowAmount(), ResponseEnum.BORROW_AMOUNT_NULL_ERROR);

        boolean result = integralGradeService.save(integralGrade);
        if (result) {
            return Result.ok().message("保存成功");
        } else {
            return Result.error().message("保持失败");
        }
    }

    @ApiOperation("根据id查询积分等级")
    @GetMapping("/get/{id}")
    public Result getById(@PathVariable Integer id) {
        IntegralGrade integralGrade = integralGradeService.getById(id);
        if (integralGrade != null) {
            return Result.ok().data("integralGrade", integralGrade);
        } else {
            return Result.error().message("数据不存在");

        }
    }

    @ApiOperation("更新积分等级")
    @PutMapping("/update")
    public Result update(@RequestBody IntegralGrade integralGrade) {
        boolean result = integralGradeService.updateById(integralGrade);
        if (result) {
            return Result.ok().message("修改成功");
        } else {
            return Result.ok().message("修改失败");
        }
    }
}

